//
//  LHAlertView.m
//  MyAlertView
//
//  Created by zhouLiHuan on 2016/12/14.
//  Copyright © 2016年 zhouLiHuan. All rights reserved.
//

#import "LHAlertView.h"

#define kviewWidth [UIScreen mainScreen].bounds.size.width
#define kviewHeight [UIScreen mainScreen].bounds.size.height


@implementation LHAlertView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        //创建遮罩
        self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kviewWidth, kviewHeight)];
        self.backView.backgroundColor = [UIColor blackColor];
        self.backView.alpha = 0.5;
        [self addSubview:self.backView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backClick)];
        [self.backView addGestureRecognizer:tap];
        
        //创建弹框视图
        self.alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 190)];
        self.alertView.center = self.center;
        self.alertView.layer.cornerRadius = 17;
        self.alertView.clipsToBounds = YES;
        self.alertView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.alertView];
        
        //弹出的动画
        [self popupAminmaView:self.alertView dur:0.5];
    }
    return self;
    
}

//点击背景
- (void)backClick{
    [self cancelView];
}

//取消按钮
- (void)cancelView{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0;
        
    }completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        self.alertView = nil;
        
    }];
    
}

//布局子控件
- (void)layoutSubviews{
    [super layoutSubviews];
    
    switch (self.type) {
        case 10:
            self.alertView.frame = CGRectMake(50, 230, 270, 190);
            [self creatOtherPayAlertView];
            break;
            
            //根据自己需求创建不同类型的弹框
        default:
            break;
    }
    [self creatBtnTitle:self.btnTitleArr];
    
}


/*可以点击其他金额的弹框*/
- (void)creatOtherPayAlertView{
    
    //其他金额按钮
    UIButton *otherMoneyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    otherMoneyBtn.frame = CGRectMake(193, 15, 60, 16);
    
    [otherMoneyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [otherMoneyBtn setTitle:@"其他按钮" forState:UIControlStateNormal];
    otherMoneyBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [otherMoneyBtn addTarget:self action:@selector(otherMoneyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:otherMoneyBtn];
    
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 88, self.contentStr.length*14+2, 16)];
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.text = self.contentStr;
    [self.alertView addSubview:contentLabel];
    
    
}

//创建弹框视图的按钮
- (void)creatBtnTitle:(NSArray *)titleArr{
    
    CGFloat m = self.alertView.frame.size.width;
    
    for (int i=0; i<self.numBtn; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (self.numBtn == 1) {
            btn.frame = CGRectMake(20, self.alertView.frame.size.height-48,(m-40), 33);
        }else{
            btn.frame = CGRectMake(20+i*(20+(m-60)/2), self.alertView.frame.size.height-48, (m-60)/2, 33);
        }
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        btn.tag = 100+i;
        btn.layer.cornerRadius = 5;
        btn.clipsToBounds = YES;
        [btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:18]];
        if ([titleArr[i] isEqualToString:@"确定"]) {
            [btn setBackgroundColor:[UIColor colorWithRed:46/255.0 green:149/255.0 blue:250/255.0 alpha:1.0]];
        }else{
            [btn setBackgroundColor:[UIColor colorWithRed:255/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]];
        }
        [self.alertView addSubview:btn];
    }
    
}

//设置弹框内容
-(void)setContentStr:(NSString *)content type:(NSInteger)type btnNum:(NSInteger)btnNum btntitleArr:(NSArray *)btnTitleArr{

    self.contentStr = content;
    self.type = type;
    self.numBtn = btnNum;
    self.btnTitleArr = btnTitleArr;
    
}

// 取消 确定按钮的点击事件
-(void)clickButton:(UIButton *)button{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickBottomBtnWithView:andClickBtn:)]) {
        
        [self.delegate clickBottomBtnWithView:self andClickBtn:button];
    }
    [self cancelView];
}

//其他金额的点击事件
- (void)otherMoneyBtnClick:(UIButton *)otherPayBtn{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickOtherPayMoneyWithView:andOtherPayMoneyBtn:)]) {
        [self.delegate clickOtherPayMoneyWithView:self andOtherPayMoneyBtn:otherPayBtn];
    }
    
    [self cancelView];
    
}

//弹出动画
- (void)popupAminmaView:(UIView *)popView dur:(CFTimeInterval)dur{
    
    CAKeyframeAnimation *animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = dur;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"];
    [popView.layer addAnimation:animation forKey:nil];
    
    
}
@end

